<!-- eslint-disable vue/valid-v-for -->
<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-button type="success" @click="handleCreate">新增</el-button> -->
      <!-- <el-input
        v-model="listQuery.name"
        placeholder="搜索姓名"
        class="filter-item"
        size="normal"
        clearable
        @keyup.enter.native="handleSearch"
      /> -->

      <!-- <el-select v-model="listQuery.status" value-key="" placeholder="请选择状态" clearable filterable @change="handleSearch">
        <el-option
          v-for="item in statusOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select> -->
      <div>
        筛选搜索
        <el-button type="primary" class="filter-button" @click="handleDownload">查询搜索</el-button>
        <el-button class="filter-button" @click="handleSearch">重置</el-button>
      </div>
      输入搜索：<el-input placeholder="订单编号" class="filter-item" />
      收货人：<el-input placeholder="收货人手机号码" class="filter-item" />
      <el-form-item label="提交时间">
        提交时间：<el-date-picker v-model="temp.submit_time" type="date" placeholder="请选择时间" />
      </el-form-item>
      提交时间：<el-input placeholder="请选择时间" class="filter-item" prop="submit_time" />
      订单状态：<el-select placeholder="订单编号" class="filter-item" />
      订单分类：<el-select placeholder="请选择时间" class="filter-item" />
      订单来源：<el-select placeholder="订单编号" class="filter-item" />
    </div>
    <div class="filter-list">数据列表</div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center">
        <template slot-scope="scope">
          {{ scope.row.num }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="提交时间">
        <template slot-scope="scope">
          <span>{{ scope.row.submit_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="订单支付" align="center">
        <template slot-scope="scope" :type="scope.row.pay | payFilter">
          {{ scope.row.pay }}
        </template>
      </el-table-column>
      <el-table-column label="订单来源" align="center">
        <template slot-scope="scope">
          {{ scope.row.source }}
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center">
        <template slot-scope="scope" :type="scope.row.status | statusFilter">
          {{ scope.row.status }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            查看订单
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row,$index)">
            删除订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogStatus === 'create' ? '新增' : '编辑'"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
      >
        <el-form-item label="订单编号" prop="num">
          <el-input v-model="temp.num" /></el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="temp.name" /></el-form-item>
        <el-form-item label="学院" prop="school">
          <el-input v-model="temp.school" /></el-form-item>
        <el-form-item label="班级" prop="class">
          <el-input v-model="temp.class" /></el-form-item>
        <el-form-item label="学籍" prop="status">
          <el-select v-model="temp.status">
            <el-option v-for="(item,index) in statusOptions" :key="index" :label="item" :value="true" />
          </el-select>
        </el-form-item>
        <el-form-item label="毕业时间" prop="graduate_time">
          <el-date-picker v-model="temp.graduate_time" type="date" placeholder="请选择时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button
          type="success"
          @click="dialogStatus==='create' ? createData() : updateData()"
        > 确定</el-button>
      </div>
    </el-dialog>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import { getList, updateStu, createStu } from '@/api/student'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '在校': 'success',
        '休学': 'gray',
        '退学': 'danger',
        '参军': 'warning'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      rules: {
        num: [{ required: true, message: '学号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }]
      },
      statusOptions: ['在校', '休学', '退学', '参军'],
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        name: '',
        status: ''
      },
      temp: {
        id: undefined,
        num: undefined,
        name: '',
        school: '',
        class: '',
        status: '',
        graduate_time: new Date()
      },
      dialogVisible: false,
      dialogStatus: 'create'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.items
        this.listLoading = false
        this.total = response.data.total
      })
    },
    handleDownload() {
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['id', 'num', 'school', 'class', 'status', 'name']
        const filterVal = ['id', 'num', 'school', 'class', 'status', 'name']
        const list = this.list
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'student-list'
        })
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          console.log('test')
        } else {
          return v[j]
        }
      }))
    },
    handleSearch() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleUpdate(row) {
      // eslint-disable-next-line no-undef
      this.temp = Object.assign({}, row)
      this.dialogVisible = true
      this.dialogStatus = 'update'
    },
    updateData() {
      const temp = Object.assign({}, this.temp)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateStu(temp).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
          })
        }
      })

      this.dialogVisible = false
      this.$notify({
        thitle: '成功',
        message: '更新成功',
        type: 'success',
        duration: 2000
      })
    },
    handleCreate() {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogStatus = 'create'
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        num: undefined,
        name: '',
        school: '',
        class: '',
        status: '',
        graduate_time: new Date()
      }
    },

    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024
          createStu(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogVisible = false
            this.$notify({
              thitle: '成功',
              message: '新增成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },

    handleDelete(row, index) {
      this.$notify({
        thitle: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    }
  }
}
</script>

  <style>
    .filter-container{
      width: 100%;
      margin-bottom: 5px;
    }
    .filter-button{
      width: 100px;
      float: right;
      margin: 10px 10px;
    }
    .filter-item{
      width: 188px;
      margin: 20px 15px;
    }
    .filter-list{
      border: 1px solid rgb(209, 209, 205);
      line-height: 30px;
      padding: 10px;
      margin-bottom:10px;
    }
  </style>

